package com.lhd.web;

import com.lhd.po.Blog;
import com.lhd.po.Tag;
import com.lhd.po.Type;
import com.lhd.service.BlogService;
import com.lhd.service.TagService;
import com.lhd.service.TypeService;
import com.lhd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhandong on 2022/4/20.
 */
@RestController
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @GetMapping("/tag-type")
    public Map getTagsAndType() {
        Map<String,Object> map = new HashMap<>();
        List<Tag> tags = tagService.listTag();
        List<Type> types = typeService.listType();
        map.put("tags",tags);
        map.put("types",types);
        return map;
    }


    @GetMapping("/getType/{page}")
    public Map getType(@PathVariable Long page){
        Map<String,Object> map = new HashMap<>();
        Integer count = typeService.countType();
        Integer rem = Math.toIntExact(count % 10);
        List<Type> types = typeService.listTypeByPage(page);
        map.put("types",types);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/10 + (rem == 0 ? 0 : 1))-1));
        map.put("curPage",page);
        return map;
    }

    @GetMapping("/types/{id}")
    public Map types(@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                     @PathVariable Long id) {

        //列出所有的type
        List<Type> types = typeService.listType();
        //给各个type添加博客数目
        for (Type type:types){
            type.setCount(typeService.countTypeByTypeId(type.getId()));
        }
        //如果id=-1且type数目大于0则激活id为首个
        if (id == -1 && types.size() > 0) {
            id = types.get(0).getId();
        }
        List<Blog> blogs = blogService.listBlogByTypeId(id,page);
        for (Blog b:blogs){
            if (b.getUserId() == null){
                b.setUser(userService.getUserById(1L));
            }else {
                b.setUser(userService.getUserById(b.getUserId()));
            }
            b.setType(typeService.getType(b.getTypeId()));
        }
        Map<String, Object> map =new HashMap<>();
        map.put("types",types);
        map.put("page",blogs);
        map.put("activeTypeId",id);

        Long count = Long.valueOf(typeService.countTypeByTypeId(id));
        Integer rem = Math.toIntExact(count % 3);

        map.put("pageNum",count/3 + (rem == 0 ? 0 : 1));
        map.put("curPage",page);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/3 + (rem == 0 ? 0 : 1))-1));
        return map;
    }
}
